<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="18" >
                <div style="margin-left:20px;font-weight: bolder;color:#666">发布消息</div>
            </el-col>

            <el-col :span="4">
                <el-button @click="go">返回列表</el-button>
            </el-col>
        </el-row>

        <el-form  ref="form" :model="form" label-position="labelPosition" label-width="100px">
            <el-form-item label="消息标题" >
                <el-input style="width:300px" v-model="form.title"></el-input>
            </el-form-item>
        </el-form>
        <el-form  label-position="labelPosition" label-width="100px">
            <el-form-item label="消息内容" >
                <div style="max-width:80%">
                    <script id="editor" type="text/plain"></script>
                </div>
            </el-form-item>
        </el-form>
        <div style="width:200px;margin:50px 100px;"><el-button  type="primary" style="width: 200px" @click="submit">发布</el-button></div>
    </div>
</template>
<script>
    import Bus from '@/config/bus.js'
    import _http from '@/components/common/http.vue'
    import _param from '@/components/common/param.vue'
    import '@/assets/ueditor/ueditor.config.js'
    import '@/assets/ueditor/ueditor.all.min.js'
    import '@/assets/ueditor/ueditor.parse.min.js'
    import '@/assets/ueditor/lang/zh-cn/zh-cn.js'
    export default {
        data() {
            return {
                page:'',
                id:'',
                apiPort:{
///                    发布内容接口
                    fabuList:"information/add",
//                    回显接口
                    getBack:"information/findById",
//                    编辑上传接口
                    editUp:'information/edit'
                },
                form:{
                    title:'',
                    content:'',
                    userid:''
                },
                activeName: 'second',
                labelPosition: 'right',
                dialogImageUrl: '',
                dialogVisible: false,
                value1: '',
            };
        },
        mounted(){
            this.id = this.$route.query.info
            this.page=this.$route.query.page
            this.userid=localStorage.getItem('userId')
            this.form.userid = localStorage.getItem('userId')
            var that = this
            this.editor = UE.getEditor('editor');
            if(this.id!=1){
                console.log(this.id)
                this.getBack(this.id)
            }
        },
        destroyed() {
            this.editor.destroy();
        },
        methods: {
            submit(){
                if(this.id!=1){
                    this.editActive()
                }else{
                    this.addActive()
                }
            },
            addMsg(){
                this.form.content=this.editor.getContent()
                var that = this
                var params = that.querystring.stringify(this.form)
                _http.axiosRequest(that,that.apiPort.fabuList,params,(res)=>{
                    console.log(res)
                    if(res.data.code==100){
                    that.$message({
                        message: '信息新增成功',
                        type: 'success'
                    });
                        setTimeout(function(){
                            that.$router.go(-1)

                        },500)
                }else{
                    that.$message({
                        message:res.data.msg,
                        type: 'warning'
                    });
                }
            })
            },
            editMsg(){
                this.form.content=this.editor.getContent()
                var that = this
                that.form.id = that.id
                var params = that.querystring.stringify(this.form)
                _http.axiosRequest(that,that.apiPort.editUp,params,(res)=>{
                    console.log(res)
                if(res.data.code==100){
                    that.$message({
                        message: '信息编辑成功',
                        type: 'success'
                    });
                    setTimeout(function(){
                        that.$router.go(-1)
                    },500)
                }else{
                    that.$message({
                        message:res.data.msg,
                        type: 'warning'
                    });
                }
            })
            },
            //            获取回显内容
            getBack(id){
                var that = this
                var params = that.querystring.stringify({'id':id,'userid':this.form.userid})
                _http.axiosRequest(that,that.apiPort.getBack,params,(res)=>{
                    that.form.title = res.data.data.title
                    that.editor.setContent(res.data.data.content)
                    console.log(res)
            })
            },
            //            发布提交
            submit(){
                if(this.id!=1){
                    this.editMsg()
                }else{
                    this.addMsg()
                }
            },
            pickerOptions0: {
                disabledDate(time) {
                    return time.getTime() < Date.now() - 8.64e7;
                }
            },
            handleClick(tab, event) {
                console.log(tab, event);
            },
            go(){
//                router.go({name: 'fabu', params: {userId: 1}});
                var that = this
                that.$router.go(-1)
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
//            handlePictureCardPreview(file) {
//                this.dialogImageUrl = file.url;
//                this.dialogVisible = true;
//            }
            send(){
                var that=this
                setTimeout(function(){
                    Bus.$emit('pageS',that.page)
                },200)
            }
        },
    };
</script>